A-hello-cmake$ tree
.
├── CMakeLists.txt
├── main.cpp
设置C++ 标准
自C 11和C 14发行以来,普遍做法是调用编译器以使用这些标准。 随着CMake的发展,有了新的使用这些标准的方式。
以下示例显示了设置C ++标准的不同方法,以及提供哪些版本的CMake。
The examples include:
-
common-method. 可以与大多数版本的CMake一起使用的简单方法。
-
cxx-standard. 使用CMake v3.1中引入的CMAKE_CXX_STANDARD变量。
-
compile-features. 使用CMake v3.1中引入的target_compile_features函数。
C++ Standard Common Method
Introduction
This example shows a common method to set the C Standard. This can be used with most versions of CMake. However, if you are targeting a recent version of CMake there are more convenient methods available.此示例显示了设置C 标准的常用方法。 可以与大多数版本的CMake一起使用。 但是,如果有CMake的最新版本建议使用其他更便捷的方法。
The files in this tutorial are below:
-
CMakeLists.txt - Contains the CMake commands you wish to run
-
main.cpp - A simple "Hello World" cpp file targeting C++11.
Concepts
Checking Compile flags
CMake has support for attempting to compile a program with any flags you pass into the function CMAKE_CXX_COMPILER_FLAG
. The result is then stored in a variable that you pass in.CMake支持传递一个变量给函数CMAKE_CXX_COMPILER_FLAG来编译程序。 然后将结果存储在您传递的变量中。
For example:
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
This example will attempt to compile a program with the flag `-std=c11` and store the result in the variable `COMPILER_SUPPORTS_CXX11`.这个例子将flag “-std=c11”传递给变量COMPILER_SUPPORTS_CXX11
The line include(CheckCXXCompilerFlag)
tells CMake to include this function to make it available for use.
想使用这个函数,必须使用include(CheckCXXCompilerFlag)包含这个函数
Adding the flag
Once you have determined if the compile supports a flag, you can then use the standard cmake methods to add this flag to a target. In this example we use the CMAKE_CXX_FLAGS
to propegate the flag to all targets .确定编译器是否支持标志后,即可使用标准cmake方法将此标志添加到目标。 在此示例中,我们使用CMAKE_CXX_FLAGS将标志(c++标准)传播给所有目标。
if(COMPILER_SUPPORTS_CXX11)#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
The above example only checks for the gcc version of the compile flags and supports fallback from C++11 to the pre-standardisation C+\0x flag. In real usage you may want to check for C14, or add support for different methods of setting the compile, e.g. `-std=gnu11`上面的示例仅检查编译标志的gcc版本,并支持从C + 11到预标准化C + \ + 0x标志的回退。 在实际使用中,您可能需要检查C14,或添加对设置编译方法的不同支持,例如 -std = gnu11
Building the Examples
Below is sample output from building this example.
$ mkdir build
$ cd build
$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Performing Test COMPILER_SUPPORTS_CXX11
-- Performing Test COMPILER_SUPPORTS_CXX11 - Success
-- Performing Test COMPILER_SUPPORTS_CXX0X
-- Performing Test COMPILER_SUPPORTS_CXX0X - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /data/code/01-basic/L-cpp-standard/i-common-method/build
$ make VERBOSE=1
/usr/bin/cmake -H/data/code/01-basic/L-cpp-standard/i-common-method -B/data/code/01-basic/L-cpp-standard/i-common-method/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
make -f CMakeFiles/hello_cpp11.dir/build.make CMakeFiles/hello_cpp11.dir/depend
make[2]: Entering directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
cd /data/code/01-basic/L-cpp-standard/i-common-method/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /data/code/01-basic/L-cpp-standard/i-common-method /data/code/01-basic/L-cpp-standard/i-common-method /data/code/01-basic/L-cpp-standard/i-common-method/build /data/code/01-basic/L-cpp-standard/i-common-method/build /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake --color=
Dependee "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/depend.internal".
Dependee "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/depend.internal".
Scanning dependencies of target hello_cpp11
make[2]: Leaving directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
make -f CMakeFiles/hello_cpp11.dir/build.make CMakeFiles/hello_cpp11.dir/build
make[2]: Entering directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
/usr/bin/cmake -E cmake_progress_report /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles 1
[100%] Building CXX object CMakeFiles/hello_cpp11.dir/main.cpp.o
/usr/bin/c++ -std=c++11 -o CMakeFiles/hello_cpp11.dir/main.cpp.o -c /data/code/01-basic/L-cpp-standard/i-common-method/main.cpp
Linking CXX executable hello_cpp11
/usr/bin/cmake -E cmake_link_script CMakeFiles/hello_cpp11.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++11 CMakeFiles/hello_cpp11.dir/main.cpp.o -o hello_cpp11 -rdynamic
make[2]: Leaving directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
/usr/bin/cmake -E cmake_progress_report /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles 1
[100%] Built target hello_cpp11
make[1]: Leaving directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
/usr/bin/cmake -E cmake_progress_start /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles 0